//
//  CancelOrderView.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/3/2.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit

class CancelOrderView: UIView, UITableViewDelegate, UITableViewDataSource, AddCornerUIRectCorner, BottomShadow {
    private let topOffset: CGFloat = 20
    private let bottomViewHeight: CGFloat = 43
    private let tableViewTopButtomOffset: CGFloat = 20
    private let cellHeight: CGFloat = 35
    private let topViewHeight: CGFloat = 36
    private let allWidth: CGFloat = 242
    private let titleDatas = ["我不想拍了", "信息填写错误", "卖家缺货", "其他原因"]
    private var title = ""
    private var isSelects = [false, false, false, false, false]
    private let mainTableView = UITableView()
    private var mainView = UIView()
    private var doneClosure:((String) -> Void)?
    private let bottomView = UIView()
    // 以后这里加一个参数传进来
    func showData(doneClosure: @escaping (String) -> Void) {
        self.doneClosure = doneClosure
        let window = UIApplication.shared.keyWindow
        self.frame = window!.bounds
        self.alpha = 0.5
        self.backgroundColor = ONColor.maskBGColor
        window?.addSubview(self)
        UIView.animate(withDuration: TimeInterval(anTIME)) {
            self.alpha = 1
        }
        
        mainView.backgroundColor = UIColor.white
        mainView.width = allWidth
        self.addSubview(mainView)
        
        let topView = UIView()
        topView.frame = CGRect(x: 0, y: 0, width: allWidth, height: topViewHeight)
        topView.backgroundColor = UIColor(hex: 0xC7C7C7)
        mainView.addSubview(topView)
        let topLabel = UILabel()
        topLabel.text = "请选择取消订单的理由"
        topLabel.font = UIFont.systemFont(ofSize: 12)
        topLabel.textAlignment = .center
        topLabel.sizeToFit()
        topLabel.frame.origin = CGPoint(x: allWidth / 2 - topLabel.width / 2, y: topView.height / 2 - topLabel.height / 2)
        topView.addSubview(topLabel)
       
        mainTableView.frame = CGRect(x: 0, y: tableViewTopButtomOffset + topView.bottom, width: allWidth, height: cellHeight * CGFloat(titleDatas.count))
        mainTableView.delegate = self
        mainTableView.dataSource = self
        mainTableView.separatorStyle = .none
        mainTableView.register(CancelOrderTableViewCell.self, forCellReuseIdentifier: "cell")
        mainView.addSubview(mainTableView)
        showBottomView(yPoint: mainTableView.bottom + topOffset)
        mainView.height = bottomView.bottom
        mainView.center = CGPoint(x: self.width / 2, y: self.height / 2 - topOffset)
        setShadow(view: mainView, opacity: bottomShadowOpacity, size: bottomShadowOffset, color: UIColor.black, cornerradius: CORNERRADIUS)
        
        addCornerWithView(aView: topView, aCorners: [.topLeft, .topRight], aSize: CGSize(width: CORNERRADIUS, height: CORNERRADIUS))
        addCornerWithView(aView: bottomView, aCorners: [.bottomLeft, .bottomRight], aSize: CGSize(width: CORNERRADIUS, height: CORNERRADIUS))
    }
    
    private func showBottomView(yPoint: CGFloat) {
        bottomView.frame = CGRect(x: 0, y: yPoint, width: mainView.width, height: bottomViewHeight)
        bottomView.backgroundColor = ONColor.searchButtomColor
        mainView.addSubview(bottomView)
        setDoneOrCancelButton()
    }
    
    /// 确认或取消
    private func setDoneOrCancelButton() {
        for index in 0...1 {
            let btnWidht: CGFloat = bottomView.width / 2
            let btn = UIButton(type: .custom)
            btn.frame = CGRect(x: CGFloat(index) * btnWidht, y: 0, width: btnWidht, height: bottomViewHeight)
            let offset = index == 0 ? -10 : 10
            btn.imageEdgeInsets = UIEdgeInsetsMake(0, CGFloat(offset), 0, 0)
            btn.backgroundColor = UIColor.clear
            btn.addTarget(self, action: #selector(doneOrCancelTap(_ :)), for: .touchUpInside)
            btn.tag = index
            let imageName = index == 0 ? "icon_wrong" : "icon_determine"
            btn.setImage(UIImage(named: imageName), for: .normal)
            bottomView.addSubview(btn)
        }
    }
    
    @objc func doneOrCancelTap(_ btn: UIButton) {
        if btn.tag == 1 {
            doneClosure?(title)
        }
        
        hide()
    }
    
    // MARK: - 消失
    private func hide() {
        UIView.animate(withDuration: TimeInterval(anTIME), animations: {
            self.alpha = 0.2
        }) { (make) in
            self.removeFromSuperview()
        }
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return titleDatas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? CancelOrderTableViewCell
        cell?.showData(title: titleDatas[indexPath.row], isSelect: isSelects[indexPath.row])
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        isSelects = isSelects.map({_ in false})
        isSelects[indexPath.row] = true
        title = titleDatas[indexPath.row]
        mainTableView.reloadData()
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return cellHeight
    }
}

class CancelOrderTableViewCell: UITableViewCell {
    let contentLeftMargin: CGFloat = 23
    let contentLabel = UILabel()
    let lineView = UIView()
    let rightImageView = UIImageView(image: UIImage(named: "btn_cancel_un_icon"), highlightedImage: UIImage(named: "btn_cancel_select_icon"))
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUI()
    }
    
    func setUI() {
        self.selectionStyle = .none
        contentLabel.font = UIFont.systemFont(ofSize: 14)
        lineView.backgroundColor = ONColor.cutLineColor
        self.addSubview(contentLabel)
        self.addSubview(rightImageView)
        self.addSubview(lineView)
    }
    
    func showData(title: String, isSelect: Bool) {
        rightImageView.isHighlighted = isSelect
        contentLabel.text = title
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        contentLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.snp.left).offset(contentLeftMargin)
            make.bottom.equalTo(self.snp.bottom).offset(-5)
        }
        
        rightImageView.snp.makeConstraints { (make) in
            make.right.equalTo(self.snp.right).offset(-13)
            make.centerY.equalTo(self.snp.centerY)
        }
        
        lineView.frame = CGRect(x: 0, y: self.height - 1, width: self.width, height: 1)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
